package com.dandelion.manager.system.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dandelion.commons.annotation.SystemLog;
import com.dandelion.commons.constants.BaseConst;
import com.dandelion.commons.web.controller.BaseController;
import com.dandelion.commons.web.model.AjaxResponse;
import com.dandelion.manager.system.domain.Resource;
import com.dandelion.manager.system.service.ResourceService;

@Controller
@RequestMapping("/manager/system/resource")
public class ResourceController extends BaseController {

	@Autowired
	public ResourceService service ;
	
	/**
	 * 主页
	 * @return
	 */
	@RequestMapping(value="/main", method=RequestMethod.GET)
	public String main() {
		return viewName("main") ;
	}
	
	/**
	 * 表单页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/form", method=RequestMethod.GET)
	public String form(Long id, Long menuId, Model model) {
		if(null != id) {
			model.addAttribute("id", id) ;
		}
		if(null != menuId) {
			model.addAttribute("menuId", menuId) ;
		}
		return viewName("form") ;
	}
	
	/**
	 * 添加
	 * @param dto
	 * @return
	 */
	@SystemLog(module=BaseConst.MODULE_SYSTEM+"[资源管理]", method="添加资源")
	@RequestMapping(value="/add", method=RequestMethod.POST)
	public @ResponseBody AjaxResponse add(Resource dto) {
		return this.service.add(dto);
	}
	
	/**
	 * 删除
	 * @param ids 数组
	 * @return
	 */
	@SystemLog(module=BaseConst.MODULE_SYSTEM+"[资源管理]", method="删除资源")
	@RequestMapping(value="/delete", method=RequestMethod.POST)
	public @ResponseBody AjaxResponse delete(Long id) {
		return this.service.delete(id);
	}
	
	/**
	 * 修改
	 * @param dto
	 * @return
	 */
	@SystemLog(module=BaseConst.MODULE_SYSTEM+"[资源管理]", method="修改资源")
	@RequestMapping(value="/update", method=RequestMethod.POST)
	public @ResponseBody AjaxResponse update(Resource dto) {
		return this.service.update(dto);
	}
	
	/**
	 * 根据ID获取数据
	 * @param id
	 * @return
	 */
	@SystemLog(module=BaseConst.MODULE_SYSTEM+"[资源管理]", method="加载资源")
	@RequestMapping(value="/get/{id}", method=RequestMethod.GET)
	public @ResponseBody Resource get(@PathVariable Long id) {
		return this.service.getResourceLinkPermit(id);
	}
	
	/**
	 * 根据导航菜单ID遍历资源 管理
	 * @param menu_id
	 * @return
	 */
	@SystemLog(module=BaseConst.MODULE_SYSTEM+"[资源管理]", method="加载资源树列表")
	@RequestMapping(value="/treegridByMenu/{menuId}")
	public @ResponseBody List<Resource> treegridByMenu(@PathVariable Long menuId) {
		return this.service.treegridByMenu(menuId) ;
	}
	
	/**
	 * 根据导航菜单ID遍历资源并关联权限
	 * @param menu_id
	 * @return
	 */
	@RequestMapping(value="/treegridByMenuLinkPermit/{menuId}")
	public @ResponseBody List<Resource> treegridByMenuLinkPermit(@PathVariable Long menuId) {
		return this.service.treegridByMenuLinkPermit(menuId);
	}
	
	/**
	 * 根据导航菜单ID遍历资源 客户展示
	 * @param menu_id
	 * @return
	 */
	@SystemLog(module=BaseConst.MODULE_SYSTEM+"[资源管理]", method="根据导航菜单ID遍历资源 客户展示")
	@RequestMapping(value="/treegridByMenuView/{menuId}")
	public @ResponseBody List<Resource> treegridByMenuView(@PathVariable Long menuId) {
		return this.service.treegridByMenuView(menuId) ;
	}
	
	/**
	 * 根据资源ID遍历
	 * @param menu_id
	 * @return
	 */
	@SystemLog(module=BaseConst.MODULE_SYSTEM+"[资源管理]", method="根据资源ID遍历")
	@RequestMapping(value="/treegridById/{id}")
	public @ResponseBody List<Resource> treegridById(@PathVariable Long id) {
		return this.service.treegridById(id) ;
	}
	
	/**
	 * 根据Href获取数据
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/getHref", method=RequestMethod.GET)
	public @ResponseBody Resource getHref(String href) {
		return this.service.selectByHref(href) ;
	}
	
}
